﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace CustomSliderLib
{
    public class CustomSlider : Slider
    {
        public static readonly DependencyProperty EmptyColorProperty = DependencyProperty.Register(
            "EmptyColor",
            typeof(Color),
            typeof(CustomSlider),
            new UIPropertyMetadata(ColorConverter.ConvertFromString("#FFE7EAEA")));

        public static readonly DependencyProperty SliderMaximumProperty = DependencyProperty.Register(
            "SliderMaximum",
            typeof (Double),
            typeof (CustomSlider));

        public static readonly DependencyProperty FillColorProperty = DependencyProperty.Register(
            "FillColor",
            typeof(Color),
            typeof(CustomSlider),
            new UIPropertyMetadata(Colors.Blue));

        
        public CustomSlider()
        {
            DataContext = this;
            var binding = new Binding();
            binding.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
            binding.Path = new PropertyPath("SliderMaximum");
            binding.Mode = BindingMode.TwoWay;
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            SetBinding(MaximumProperty, binding);
        }

        private static void OnSliderMaximumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as CustomSlider;
            if (control != null)
                control.Maximum = (Double)e.NewValue;
        }
        
        static CustomSlider()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomSlider), new FrameworkPropertyMetadata(typeof(CustomSlider)));
        }

        public double SliderMaximum
        {
            get { return (double) GetValue(SliderMaximumProperty); }
            set { SetValue(SliderMaximumProperty, value); }
        }

        public Color EmptyColor
        {
            get { return (Color)GetValue(EmptyColorProperty); }
            set { SetValue(EmptyColorProperty, value); }
        }
        
        public Color FillColor
        {
            get { return (Color)GetValue(FillColorProperty); }
            set { SetValue(FillColorProperty, value); }
        }
    }
}
